package site.hanzhe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.hanzhe.constant.CrowdConstant;
import site.hanzhe.dao.RoleDao;
import site.hanzhe.entity.RoleEntity;
import site.hanzhe.service.RoleService;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageInfo<RoleEntity> selectPageInfo(Integer current, Integer size, String query) {
        PageHelper.startPage(current, size);
        List<RoleEntity> list = roleDao.selectList(new QueryWrapper<RoleEntity>()
                .like("name", query));
        return new PageInfo<>(list, CrowdConstant.PAGE_NAVIGATE_LENGTH);
    }

    @Override
    public List<RoleEntity> selectUnAssignRoleByAdminId(Integer id) {
        return roleDao.selectUnAssignRoleByAdminId(id);
    }

    @Override
    public List<RoleEntity> selectAssignRoleByAdminId(Integer id) {
        return roleDao.selectAssignRoleByAdminId(id);
    }

    @Override
    public int insertRole(String name) {
        return roleDao.insert(new RoleEntity(null, name));
    }

    @Override
    public int updateRole(RoleEntity entity) {
        return roleDao.updateById(entity);
    }

    @Override
    public int deleteRoleByIdList(List<String> list) {
        return roleDao.deleteBatchIds(list);
    }





    @Override
    public int deleteAssignRoleByAdminId(Integer id) {
        return roleDao.deleteAssignRoleByAdminId(id);
    }

    @Override
    public int insertAssignRoleByAdminId(Integer id, List<String> list) {
        // 只有在集合不为空的情况下才执行插入 否则SQL报错
        return ( list.size() > 0 ) ? roleDao.insertAssignRoleByAdminId(id, list) : 0;
    }

}